package dymnd;

import java.util.Hashtable;
import java.util.LinkedList;

import dymnd.image.Layer;

public class LayerHistoryManager {
	private LinkedList<LinkedList<Layer>> layers = new LinkedList<LinkedList<Layer>>();
	private Hashtable<String, Integer> userUndoIndices = new Hashtable<String, Integer>();
	int stepLimit = 10;
	public LayerHistoryManager()	{
		
	}
	
	public void onDo(Layer l)	{
		//make sure the layer is not null
		if(l == null)
			throw new NullPointerException("Layer cannot be null!!");
		
		//make sure this is a non-public layer... 
		if(l.isPublic())
			throw new IllegalArgumentException("Layer cannot be public for undo to work correctly.");
		else	{
			if(userExists(l.getOwner()))	{
				
				//layers.get(getUserIndex(l.getOwner()))
				Client.debugMsg("clientExists, writing layer...");
				layers.get(getUserIndex(l.getOwner())).add(l.clone());
				removeToIndexFromLast(getUserIndex(l.getOwner()), userUndoIndices.get(l.getOwner()));
				int index = userUndoIndices.remove(l.getOwner());
				//removeToIndexFromLast(getUserIndex(l.getOwner()), );
				//removeToIndexFromLast(getUserIndex(l.getOwner()), index);
				index++;
				
				userUndoIndices.put(l.getOwner(), new Integer(index));
			}	else	{
				Client.debugMsg("Client does not exist, adding new client");
				LinkedList<Layer> userLayers = new LinkedList<Layer>();
				userLayers.add(l.clone());
				layers.add(userLayers);
				//layers.get(getUserIndex(l.getOwner())).add(l);
				userUndoIndices.put(l.getOwner(), new Integer(0));
				
				
			}
			Client.debugMsg("trimmed to size " + layers.get(0).size());
			//trimToStepLimit();
			
		}
		
	}	
	
	
	private void trimToStepLimit()	{
		for(int i = 0; i < layers.size(); i++)	{
			while(layers.get(i).size() > stepLimit)	{
				layers.get(i).removeFirst();
			}
			Client.debugMsg("trimmed to size " + layers.get(i).size());
		}
		
	}
	
	private void removeToIndexFromLast(int userIndex, int index)	{
		while(index < layers.get(userIndex).size()-1)	{
			layers.get(userIndex).removeLast();
		}
	}
	
	public Layer requestNextUndoLayer(String user)	{
		if(userExists(user))	{
			if(userUndoIndices.get(user)+1 >= 0)	{
				userUndoIndices.put(user, userUndoIndices.get(user)-1);
				return layers.get(getUserIndex(user)).get(userUndoIndices.get(user));
			}
		}	else
			throw new IllegalArgumentException("Specified user does not exist!");
		return null;
	}
	
	public Layer requestNextRedoLayer(String user)	{
		return null;
	}
	
	private boolean userExists(String user)	{
		for(int i = 0; i < layers.size(); i++)	{
			if(layers.get(i).get(0).getOwner() != null && layers.get(i).get(0).getOwner().equalsIgnoreCase(user)){
					return true;
			}
		}
		return false;
	}
	private int getUserIndex(String user)	{
			for(int i = 0; i < layers.size(); i++)	{
				if(layers.get(i).size() > 0)
					if(layers.get(i).get(0).getOwner() != null && layers.get(i).get(0).getOwner().equalsIgnoreCase(user)){
							return i;
					}
			}
		return -1;
	}
	
}
